package com.hpeng.daily.action;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hpeng.daily.service.AssetCardManager;
import com.hpeng.gl.service.DepartmentService;
import com.hpeng.sys.model.AssetCard;
import com.hpeng.sys.model.Staff;
import com.hpeng.sys.service.Function;
import com.hpeng.sys.service.LoginUserMgr;
import com.hpeng.sys.vo.Lookup;
import com.hpeng.util.Pagination;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Component("assetCardExternalAction")
@Scope("prototype")
public class AssetCardExternalAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	// 用于分页显示
	private int page;
	public String pageLink;
	private Pagination<AssetCard> pagination;
	private List<Lookup> ls;//用于页面上显示资产的状态的list
	
	//用于根据权限生成功能按钮
	private String FN;
	private String accessRight;
	
	@Resource
	private Function function;
	@Resource
	private LoginUserMgr loginUser;
	//资产卡片的业务层
	@Resource
	private AssetCardManager assetCardManager;
	@Resource
	private DepartmentService departmentService;
	private AssetCard assetCard;	//资产卡片实体
	private String acid;	//资产卡片的编号id
	private String catid;
	private String idArray; 
	private String msg;
	private String result;
	private String depPassname;
	private String depid;
	private Set<Staff> setStaffs;
	private String newuser;
	
	
	//条件查询
	private String conditionValue;
	private String condition;
	private String acstatusValue;
	
	
	/************************资产外部调动*************************************/
	//公共方法  用于得到登录人所在的部门名称的
	public String getDepname(){
		HttpServletRequest request = ServletActionContext.getRequest();
		return loginUser.getUserDeptName(request);
	}
	//新部门经办进行物资分配时  得到该部门下的员工集合
	public String getStaffs(){
		String depid = (String)ActionContext.getContext().getSession().get("depid");
		System.out.println("转交给了部门的编号是："+depid);
		setStaffs = departmentService.findDepartmentById(depid).getStaffs();
		return "success";
	}
	//得到外调的资产
	 public String searchExternal(){
			//查询所有状态为在用与内调的资产卡片
			String[] hql = { " from AssetCard where depname='"+getDepname()+"' and acstatus='400' or acstatus like '8%' "};
			String order = " ";
			setPagination(assetCardManager.getPagination(page, hql, order));
			pageLink = pagination.getPageLink("searchExternal.action?FN=" + FN);
			this.accessRight = function.getAccessRight(session(), FN);
			ls = function.getLookup("prod_status_mst", "status_code||sub_code",
					"status_desc||' - '||sub_desc");
			return "success";
		}
	//点击树上的资产类别 得到对应的资产情况	
		public String queryExternalAssetCardByCategory(){
			String[] hql = { " from AssetCard where depname='"+getDepname()+"' and acstatus='400' and catid like '"
					+ catid + "%' or catid like '" + catid + "%' and acstatus like '8%' " };
			String order = " ";
			setPagination(assetCardManager.getPagination(page, hql, order));
			pageLink = pagination.getPageLink("queryExternalAssetCardByCategory.action?FN=" + FN
					+ "&catid=" + catid);
			ls = function.getLookup("prod_status_mst", "status_code||sub_code",
					"status_desc||' - '||sub_desc");
			this.accessRight = function.getAccessRight(session(), FN);
			return "success";
		}
   // 条件查询
		public String searchExternalByCondition(){
			try {
				if(conditionValue!=null){
				       conditionValue = new String(conditionValue.getBytes("ISO8859-1"),
						"UTF-8");
				       }
				String append = "";
				if ("zclb".equals(condition)) {
					append = " and category.catname like ('%"+conditionValue+"%') ";
				} else if ("zczt".equals(condition)) {
					append = " and acstatus like ('%"+acstatusValue+"%') ";
				} 
				String[] hql = {
						" from AssetCard where depname='"+getDepname()+"'" ,
						//+" and acstatus like '"+ 400 + "' or acstatus like '" + 8 + "%' ",
						append };
				String order = " order by acid asc";
				setPagination(assetCardManager.getPagination(page, hql, order));
				pageLink = pagination
						.getPageLink("searchExternalByCondition.action?condition=" + condition
								+ "&conditionValue=" + conditionValue+"&acstatusValue=" + acstatusValue);
				this.accessRight = function.getAccessRight(session(), FN);
				ls = function.getLookup("prod_status_mst", "status_code||sub_code",
						"status_desc||' - '||sub_desc");
			} catch (UnsupportedEncodingException e) {
				System.out.println(e.getMessage());
			}
			return "success";
		}
  //  外调-原部门经办已申请
		public String updateStatusByJBAsk(){
			try{
				String[] stringArray = idArray.split(",");
			for (int i = 0; i < stringArray.length; i++) {
				assetCard = assetCardManager.findAssetCard(stringArray[i]);
				function.updStatus("8", "60", stringArray);
				assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),null,function.getStatusDesc("8", "60"));
			   }
			} catch (Exception rx) {
				msg = getText("申请失败，请重新操作");
			}
			if (msg == null || "".equals(msg.trim())) {
				msg = getText("申请成功");
			}
			this.accessRight = function.getAccessRight(session(), FN);
			ls = function.getLookup("prod_status_mst", "status_code||sub_code",
					"status_desc||' - '||sub_desc");
			return searchExternal();
		}
	//外调-原部门经办已上报
		public String updateStatusByJBReport(){
			try{
				String[] stringArray = idArray.split(",");
			for (int i = 0; i < stringArray.length; i++) {
				assetCard = assetCardManager.findAssetCard(stringArray[i]);
				if(assetCard.getAcstatus().equals("860")){
				     function.updStatus("8", "61", stringArray);
				     assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),null,function.getStatusDesc("8", "61"));
				  }else{
					msg=getText("部门经办只能上报处于部门经办已申请的资产");
				  }
			   }
			} catch (Exception rx) {
				msg = getText("上报失败，请重新操作");
			}
			if (msg == null || "".equals(msg.trim())) {
				msg = getText("上报成功");
			}
			this.accessRight = function.getAccessRight(session(), FN);
			ls = function.getLookup("prod_status_mst", "status_code||sub_code",
					"status_desc||' - '||sub_desc");
			return searchExternal();
		}
	//外调-原部门领导已审核
		public String updateStatusByOldLDAudit(){
			try{
				String[] stringArray = idArray.split(",");
			for (int i = 0; i < stringArray.length; i++) {
				assetCard = assetCardManager.findAssetCard(stringArray[i]);
				if(assetCard.getAcstatus().equals("861")){
				     function.updStatus("8", "62", stringArray);
				     assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),result,function.getStatusDesc("8", "62"));
				  }else{
					msg=getText("部门领导只能审核部门经办已上报的资产");
				  }
			   }
			} catch (Exception rx) {
				msg = getText("审核失败，请重新操作");
			}
			if (msg == null || "".equals(msg.trim())) {
				msg = getText("审核成功");
			}
			this.accessRight = function.getAccessRight(session(), FN);
			ls = function.getLookup("prod_status_mst", "status_code||sub_code",
					"status_desc||' - '||sub_desc");
			return searchExternal();
		}
		//外调--原部门领导审核退回
		public String updateStatusByOldLDBack(){
			try{
				String[] stringArray = idArray.split(",");
			for (int i = 0; i < stringArray.length; i++) {
				assetCard = assetCardManager.findAssetCard(stringArray[i]);
				if(assetCard.getAcstatus().equals("861")){
				     function.updStatus("8", "60", stringArray);
				     assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),result,function.getStatusDesc("8", "60"));
				  }else{
					msg=getText("部门领导只能退回部门经办已上报的资产");
				  }
			   }
			} catch (Exception rx) {
				msg = getText("退回失败，请重新操作");
			}
			if (msg == null || "".equals(msg.trim())) {
				msg = getText("退回成功");
			}
			this.accessRight = function.getAccessRight(session(), FN);
			ls = function.getLookup("prod_status_mst", "status_code||sub_code",
					"status_desc||' - '||sub_desc");
			return searchExternal();
		}
//	外调-原部门领导已转交
		public String updateStatusByOldLDPass(){
			try{
				Map<String, Object> session = ActionContext.getContext().getSession();
				session.put("depid", depid);
				String[] stringArray = idArray.split(",");
			for (int i = 0; i < stringArray.length; i++) {
				assetCard = assetCardManager.findAssetCard(stringArray[i]);
				if(assetCard.getAcstatus().equals("862")){
				     function.updStatus("8", "63", stringArray);
				     assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),null,function.getStatusDesc("8", "63"));
				  }else{
					msg=getText("部门领导只能转交状态为外调--部门领导已审核的资产");
				  }
			   }
			} catch (Exception rx) {
				msg = getText("转交失败，请重新操作");
			}
			if (msg == null || "".equals(msg.trim())) {
				msg = getText("转交成功");
			}
			this.accessRight = function.getAccessRight(session(), FN);
			ls = function.getLookup("prod_status_mst", "status_code||sub_code",
					"status_desc||' - '||sub_desc");
			return searchExternal();
		}
	//外调-新部门领导已审核
		public String updateStatusByNewLDAudit(){
			try{
				String[] stringArray = idArray.split(",");
			for (int i = 0; i < stringArray.length; i++) {
				assetCard = assetCardManager.findAssetCard(stringArray[i]);
				if(assetCard.getAcstatus().equals("863")){
				     function.updStatus("8", "64", stringArray);
				     assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),result,function.getStatusDesc("8", "64"));
				  }else{
					msg=getText("部门只能审核外调--原部门领导已转交");
				  }
			   }
			} catch (Exception rx) {
				msg = getText("审核失败，请重新操作");
			}
			if (msg == null || "".equals(msg.trim())) {
				msg = getText("审核成功");
			}
			this.accessRight = function.getAccessRight(session(), FN);
			ls = function.getLookup("prod_status_mst", "status_code||sub_code",
					"status_desc||' - '||sub_desc");
			return searchExternal();
		}
		//外调-新部门领导退回资产
		public String updateStatusByNewLDBack(){
			try{
				String[] stringArray = idArray.split(",");
			for (int i = 0; i < stringArray.length; i++) {
				assetCard = assetCardManager.findAssetCard(stringArray[i]);
				if(assetCard.getAcstatus().equals("863")){
				     function.updStatus("8", "62", stringArray);
				     assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),result,function.getStatusDesc("8", "62"));
				  }else{
					msg=getText("部门领导只能退回外调--原部门领导已转交");
				  }
			   }
			} catch (Exception rx) {
				msg = getText("退回失败，请重新操作");
			}
			if (msg == null || "".equals(msg.trim())) {
				msg = getText("退回成功");
			}
			this.accessRight = function.getAccessRight(session(), FN);
			ls = function.getLookup("prod_status_mst", "status_code||sub_code",
					"status_desc||' - '||sub_desc");
			return searchExternal();
		}
	//外调-新部门领导已下传
		public String updateStatusByNewLDDownload(){
			try{
				String[] stringArray = idArray.split(",");
			for (int i = 0; i < stringArray.length; i++) {
				assetCard = assetCardManager.findAssetCard(stringArray[i]);
				if(assetCard.getAcstatus().equals("864")){
				     function.updStatus("8", "65", stringArray);
				     assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),null,function.getStatusDesc("8", "65"));
				  }else{
					msg=getText("部门领导只能下传状态为外调--新部门领导已审核的资产");
				  }
			   }
			} catch (Exception rx) {
				msg = getText("下传失败，请重新操作");
			}
			if (msg == null || "".equals(msg.trim())) {
				msg = getText("下传成功");
			}
			this.accessRight = function.getAccessRight(session(), FN);
			ls = function.getLookup("prod_status_mst", "status_code||sub_code",
					"status_desc||' - '||sub_desc");
			return searchExternal();
		}
		//外调--部门经办进行个人分配
		public String updateExternalAllotPersonal(){
			try{
				String[] stringArray = idArray.split(",");
				String depid = (String)ActionContext.getContext().getSession().get("depid");
				String depname = departmentService.findDepartmentById(depid).getDepname();
			for (int i = 0; i < stringArray.length; i++) {
				assetCard = assetCardManager.findAssetCard(stringArray[i]);
				if(assetCard.getAcstatus().equals("865")){//此时资产状态变为在用
					 assetCard.setIspublic("N");//此时  资产公共状态变为N 为不公用
					 assetCard.setAcuser(newuser);//设置现在使用人
					 assetCard.setDepname(depname);//设置现在使用部门
					 assetCard.setAcstatus("400");
				     assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),null,function.getStatusDesc("4", "00"));
				   if(assetCard.getAcstatus().equals("Y")){
					   assetCard.setDepname(assetCard.getReportdepartment());
					   assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),null,function.getStatusDesc("4", "00"));
					   msg = getText("资产属于公用资产，已经保存");
				   } 
				}else{
					msg=getText("分配的只能是状态处于外调部门领导已下传的资产");
				  }
			   }
			} catch (Exception rx) {
				msg = getText("分配资产失败，请重新操作");
			}
			if (msg == null || "".equals(msg.trim())) {
				msg = getText("分配资产成功");
			}
			this.accessRight = function.getAccessRight(session(), FN);
			ls = function.getLookup("prod_status_mst", "status_code||sub_code",
					"status_desc||' - '||sub_desc");
			return searchExternal();
		}
		
		
		
		
		
  /***************************set   get方法*************************************/
		public Pagination<AssetCard> getPagination() {
			return pagination;
		}
		public void setPagination(Pagination<AssetCard> pagination) {
			this.pagination = pagination;
		}
		public void setPage(int page) {
			this.page = page;
		}
		public void setAssetCard(AssetCard assetCard) {
			this.assetCard = assetCard;
		}
		public void setAcid(String acid) {
			this.acid = acid;
		}
		public void setFN(String fN) {
			FN = fN;
		}
		public String getPageLink() {
			return pageLink;
		}
		public void setPageLink(String pageLink) {
			this.pageLink = pageLink;
		}
		public int getPage() {
			return page;
		}
		public String getFN() {
			return FN;
		}
		public AssetCard getAssetCard() {
			return assetCard;
		}
		public String getAcid() {
			return acid;
		}
		public String getAccessRight() {
			return accessRight;
		}
		public void setAccessRight(String accessRight) {
			this.accessRight = accessRight;
		}
		public List<Lookup> getLs() {
			return ls;
		}
		public void setLs(List<Lookup> ls) {
			this.ls = ls;
		}
		public Map<String,Object> session(){
			return ActionContext.getContext().getSession();
		}
		public String getCatid() {
			return catid;
		}
		public void setCatid(String catid) {
			this.catid = catid;
		}
		public String getIdArray() {
			return idArray;
		}
		public void setIdArray(String idArray) {
			this.idArray = idArray;
		}
		public String getMsg() {
			return msg;
		}
		public void setMsg(String msg) {
			this.msg = msg;
		}
		public HttpServletRequest request(){
			return ServletActionContext.getRequest();
		}
		public String getConditionValue() {
			return conditionValue;
		}
		public void setConditionValue(String conditionValue) {
			this.conditionValue = conditionValue;
		}
		public String getCondition() {
			return condition;
		}
		public void setCondition(String condition) {
			this.condition = condition;
		}
		public String getAcstatusValue() {
			return acstatusValue;
		}
		public void setAcstatusValue(String acstatusValue) {
			this.acstatusValue = acstatusValue;
		}
		public String getResult() {
			return result;
		}
		public void setResult(String result) {
			this.result = result;
		}
		public String getDepPassname() {
			return depPassname;
		}
		public void setDepPassname(String depPassname) {
			this.depPassname = depPassname;
		}
		public String getDepid() {
			return depid;
		}
		public void setDepid(String depid) {
			this.depid = depid;
		}
		public Set<Staff> getSetStaffs() {
			return setStaffs;
		}
		public void setStaffs(Set<Staff> setStaffs) {
			this.setStaffs = setStaffs;
		}
		public String getNewuser() {
			return newuser;
		}
		public void setNewuser(String newuser) {
			this.newuser = newuser;
		}
}
